package com.simen.connectandroid.command.utils;

import com.simen.connectandroid.model.Device;
import com.simen.hellobytes.StringUtil;

/**
 * Created on 2021/10/30 15:36.
 *
 * @author Simen
 */
public class CommandCreator {

    public static CommandCreator createAdb() {
        return create("adb");
    }

    public static CommandCreator create(String command) {
        return new CommandCreator(command);
    }

    private String command;
    private StringBuilder commandBuilder = new StringBuilder();

    public CommandCreator(String command) {
        this.command = command;
        this.commandBuilder = new StringBuilder(command);
    }

    public CommandCreator appendParam(String... params) {
        return appendParam(true, params);
    }

    public CommandCreator appendParam(boolean trim, String... params) {
        for (int i = 0; i < params.length; i++) {
            if (StringUtil.isTrimNotEmpty(params[i])) {
                commandBuilder.append(" ");
                commandBuilder.append(trim ? params[i].trim() : params[i]);
            }
        }
        return this;
    }

    public CommandCreator appendAdbDevice(Device device) {
        if (device != null) {
            appendParam("-s", device.getAdbDeviceName());
        }
        return this;
    }

    public String build() {
        return commandBuilder.toString();
    }
}
